﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Eacademy.Domain.Abstractions;
using Eacademy.Domain.Models;
using Eacademy.Domain.Repository;
using Eacademy.Library.Actions;

namespace Eacademy.Areas.Admin.Controllers
{
    [CustomAuthorize(Roles = "Admin")]
    public class SubjectsController : Controller
    {

        private const int DefaultPageSize = 10;
        private ISubjectRepository _subjectRepository;
        private IMentorRepository _mentorRepository;
        private ILessonRepository _lessonRepository;

        public SubjectsController(ISubjectRepository subjectRepository,IMentorRepository mentorRepository, ILessonRepository lessonRepository)
        {
            _subjectRepository = subjectRepository;
            _mentorRepository = mentorRepository;
            _lessonRepository = lessonRepository;
        }

        //
        // GET: /Admin/Subjects/

        public ActionResult Index(int? page)
        {
            int currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0;
            SubjectListFilterViewModel subjectListFilterViewModel = new SubjectListFilterViewModel();
            AdminSubjectListViewModel adminSubjectListViewModel = new AdminSubjectListViewModel();
            int totalcount = 0;
            List<SubjectCompactViewModel> subjectList = _subjectRepository.GetSubjects(currentPageIndex + 1, DefaultPageSize,0, subjectListFilterViewModel, out totalcount);
            adminSubjectListViewModel.SubjectList = subjectList.ToPagedList(currentPageIndex, DefaultPageSize, totalcount);

            return View(adminSubjectListViewModel);
        }

        public ActionResult Add()
        {
            AdminSubjectsViewModel adminSubjectsViewModel = new AdminSubjectsViewModel();
            return View(adminSubjectsViewModel);
        }

        [HttpPost]
        public ActionResult Add(AdminSubjectsViewModel adminSubjectsViewModel, string mentors)
        {
            adminSubjectsViewModel.MentorIdList = mentors;

            //if (_subjectRepository.AddSubject(subjectCompactViewModel))
            //{
            //    return Redirect("/admin/subjects");
            //}

            if (ModelState.IsValid)
            { 
                _subjectRepository.AddSubject(adminSubjectsViewModel);
                return Redirect("/admin/subjects");
            }

            return View(adminSubjectsViewModel);

        }

        public ActionResult Edit(int id)
        {
            AdminSubjectsViewModel adminSubjectsViewModel = _subjectRepository.GetSubjectBySubjectId(id);
            return View(adminSubjectsViewModel);

        }

        [HttpPost]
        public ActionResult Edit(AdminSubjectsViewModel adminSubjectsViewModel, string mentors)
        {
            adminSubjectsViewModel.MentorIdList = mentors;

            if (ModelState.IsValid)
            {
                _subjectRepository.UpdateSubject(adminSubjectsViewModel);
                this.FlashDone("The subject has been updated");
                return Redirect("/admin/subjects");
            }

            return View(adminSubjectsViewModel);

        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            if (_subjectRepository.DeleteSubject(id))
            {
                this.FlashDone("The subject and lessons under it has been deleted successfully");
            }
            else
            {
                this.FlashError("There was an error deleting the subject");
            }
            return Content("Subject Delete");
        }

        public ActionResult GetMentors(string q)
        {
            MentorFilterViewModel mentorFilterViewModel = new MentorFilterViewModel();
            mentorFilterViewModel.Name = q;
            var mentors = _mentorRepository.GetMentors(mentorFilterViewModel).Select(mentor => new
            {
                id = mentor.MentorId,
                name = String.Format("{0} {1} {2}", mentor.FirstName,mentor.MiddleName,mentor.LastName)
            }).ToArray();

            return Json(mentors, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetMentorsBySubject(int id)
        {
            var mentors = _mentorRepository.GetMentorsBySubject(id).Select(mentor => new
            {
                id = mentor.MentorId,
                name = String.Format("{0} {1} {2}", mentor.FirstName, mentor.MiddleName, mentor.LastName)
            }).ToArray();

            return Json(mentors, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetMentorsBySubjectId(int id)
        {
            var mentors = _mentorRepository.GetMentorsBySubject(id);
            if (mentors.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<ul>");
                foreach (var mentor in mentors)
                {
                    sb.AppendLine("<li>");
                    sb.AppendLine(String.Format("<input type=\"radio\" name=\"mentor\" value=\"{0}\">",
                                                mentor.UserId));
                    sb.AppendLine(String.Format("<span class='mentor-name'>{0} {1}</span>", mentor.FirstName,mentor.LastName));
                    sb.AppendLine("</li>");
                }
                sb.AppendLine("</ul>");

                return Content(sb.ToString());
            }
            Response.TrySkipIisCustomErrors = true;
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return Content("<p>No mentors for this subject</p>");
        }

        //public ActionResult Lessons(int id,int? page)
        //{
        //    int currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0;
        //    AdminLessonListViewModel adminLessonListViewModel = new AdminLessonListViewModel();
        //    int totalcount = 0;
        //    List<AdminLessonsViewModel> lessonList = _lessonRepository.GetLessonsBySubjectId(id, currentPageIndex + 1, DefaultPageSize, out totalcount);
        //    adminLessonListViewModel.LessonsList = lessonList.ToPagedList(currentPageIndex, DefaultPageSize, totalcount);
        //    adminLessonListViewModel.Subject = _subjectRepository.GetSubjectBySubjectId(id);
        //    return View(adminLessonListViewModel);
        //}
    }
}
